<template>
  <div class="app-container" v-loading="loading">
    <!--   搜索选项   -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试ID" prop="examId">
        <el-select clearable v-model="queryParams.examId" placeholder="请选择考试" style="width:100%" @change="handleQuery()">
          <el-option
            v-for="item in psDeptAndExamSelectData.examList"
            :key="item.key"
            :value="item.key"
            :label="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select clearable v-model="queryParams.deptId" placeholder="请选择考试" style="width:100%" @change="handleQuery()">
          <el-option
            v-for="item in psDeptAndExamSelectData.deptList"
            :key="item.key"
            :value="item.key"
            :label="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-row :gutter="15">
        <el-col :sm="8" v-for="deptData in examDeptStatusList">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{deptData.deptId}} {{deptData.deptName}}</span>
            </div>
            <!--      内容      -->
            <div class="scrollbar">
              <div v-for="(examData, examIndex) in deptData.examDeptStatusList"
                   :key="examData.examId +':' +examData.repeatCount"
                   v-loading="examData.loading">
                <div>
                  <span style="margin-right: 15px;"><strong>第{{examData.repeatCount}}次考试</strong></span>
                  <el-tag
                    v-if="examData.status === 2"
                    >
                    已结束
                  </el-tag>
                  <el-switch
                    v-if="examData.status !== 2"
                    v-model="examData.status"
                    @change="toNextExamStatus(examData)"
                    size="mini"
                    :inactive-value="0"
                    :active-value="1"
                    active-text="开启"
                    inactive-text="关闭">
                  </el-switch>
                </div>
                <div>

                </div>
                <!--      考试时间    -->
                <div>
                  <div v-if="examData.startTime">
                    <p>时间：
                      <span style="margin: 0 5px;">{{examData.startTime}}</span>
                      <span style="margin: 0 5px;">{{examData.endTime}}</span>
                    </p>
                  </div>
                  <!--       时间控制         -->
                  <div v-if="examData.status === 0">
                    <el-button type="text"
                               @click="$set(examData, 'dateDialogVisible', true)"
                               v-if="examData.status === 0">
                      设置考试自动开始时间
                    </el-button>
                    <div v-show="examData.dateDialogVisible">
                        <el-date-picker
                          v-model="examData.startTime"
                          format="yy-MM-dd HH:mm"
                          value-format="yy-MM-dd HH:mm"
                          type="datetime"
                          @change="changeExamStartTime(examData, deptData.totalTime)"
                          placeholder="选择日期时间">
                        </el-date-picker>
                    </div>
                  </div>
                </div>
                <el-divider v-if="examIndex !== (deptData.examDeptStatusList.length -1)"></el-divider>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { getExamDeptStatus,
  addExamDeptStatus, updateExamDeptStatus, deptExamView, updateStartTime,
  nextStatus
} from "@/api/psedu-exam/examDeptStatus";
import { psDeptAndExamOption } from "@/api/psedu-base/selectOption"
import moment from 'moment';
export default {
  name: "ExamDeptStatus",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      psDeptAndExamSelectData: {
        deptList: [],
        examList: []
      },
      examDeptStatusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateDialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getPsDeptAndExamOption().then(() => {
      this.getList();
    });
  },
  methods: {
    getPsDeptAndExamOption() {
      this.loading = true;
      return psDeptAndExamOption().then(res => {
        this.loading = false;
        let data = res.data;
        this.psDeptAndExamSelectData.deptList = data.deptList;
        this.psDeptAndExamSelectData.examList = data.examList;
        this.queryParams.examId = data.examList[0].key;
        if(this.psDeptAndExamSelectData.deptList.length === 1) {
          this.queryParams.deptId = data.deptList[0].key;
        }
        return Promise.resolve();
      })
    },
    /** 查询分党校考试信息列表 */
    getList() {
      this.loading = true;
      deptExamView(this.queryParams.examId, this.queryParams.deptId).then(response => {
        this.examDeptStatusList = response.data;
        this.loading = false;
      });
    },
    /** 下一个考试状态 */
    toNextExamStatus(examDept) {
      this.$set(examDept, "loading", true);
      nextStatus(examDept.examDeptStatusId).then(res => {
        examDept.startTime = res.data.startTime;
        examDept.endTime = res.data.endTime;
        examDept.status = res.data.status;
        examDept.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examDeptStatusId: null,
        examId: null,
        repeatCount: null,
        startTime: null,
        endTime: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    changeExamStartTime(examData, totalTime) {
      this.$set(examData, "loading",  true);
      console.log(examData);
      updateStartTime({examDeptStatusId: examData.examDeptStatusId, startTime: examData.startTime}).then(res => {
        console.log(res);
        examData.loading = false;
        console.log(examData.loading);
        examData.endTime = moment(examData.startTime, "YY-MM-DD HH:mm")
          .add(totalTime, 'm')
          .format('YY-MM-DD HH:mm');
        console.log(examData.endTime);
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examDeptStatusId = row.examDeptStatusId || this.ids
      getExamDeptStatus(examDeptStatusId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分党校考试信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examDeptStatusId != null) {
            updateExamDeptStatus(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExamDeptStatus(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped>
  .scrollbar {
    overflow-y: auto;
    max-height: 180px;
    min-height: 180px;
  }
  .scrollbar::-webkit-scrollbar {
    /*滚动条整体样式*/
    width : 5px;  /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
  }
  .scrollbar::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 5px;
    box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    background   : #dddddd;
  }
  .scrollbar::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    box-shadow   : inset 0 0 5px rgba(0, 0, 0, 0.2);
    border-radius: 5px;
    background   : #ededed;
  }
</style>
